﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Core.Elements
{
    public class HttpResponsePacket
    {
        public List<HttpHeader> lstHeaders;
        public string version = string.Empty;
        public string errorCode = string.Empty;
        public string message = string.Empty;
        public byte[] data = null;

        public bool dropped = false; // set it to drop the packet

        public HttpResponsePacket(string version, string errorCode, string message)
        {
            this.version = version;
            this.errorCode = errorCode;
            this.message = message;

            lstHeaders = new List<HttpHeader>();
        }

        public void SetData(byte[] data)
        {
            this.data = data;
        }

        public Elements.HttpHeaderContentType GetContentType()
        {
            foreach (HttpHeader header in lstHeaders)
            {
                if (header.name == "Content-Type")
                {
                    Elements.HttpHeaderContentType contentType = new HttpHeaderContentType(header.value);
                    return contentType;
                }
            }

            return null;
        }

        public void AddHeader(HttpHeader header)
        {
            lstHeaders.Add(header);
        }

        public HttpHeader[] GetHeaders(string name, bool caseSensitive)
        {
            int elements = NumberOfHeaderOcurrences(name, caseSensitive);

            HttpHeader[] newList = new HttpHeader[elements];
            int e = 0;
            for (int i = 0; i < lstHeaders.Count; i++)
            {
                if (caseSensitive && lstHeaders[i].name == name)
                {
                    newList[e] = lstHeaders[i];
                    e++;
                }
                else if (!caseSensitive && lstHeaders[i].name.ToUpper() == name.ToUpper())
                {
                    newList[e] = lstHeaders[i];
                    e++;
                }
            }

            return newList;
        }

        /// <summary>
        /// Also, remove duplicates
        /// </summary>
        /// <param name="name"></param>
        /// <param name="caseSensitive"></param>
        /// <returns></returns>
        public bool RemoveHeader(string name, bool caseSensitive)
        {
            bool remove = false;
            bool removed = false;
            for (int i = lstHeaders.Count - 1; i >= 0; i--)
            {
                if (caseSensitive && lstHeaders[i].name == name)
                    remove = true;
                else if (!caseSensitive && lstHeaders[i].name.ToUpper() == name.ToUpper())
                    remove = true;

                if (remove)
                {
                    lstHeaders.Remove(lstHeaders[i]);
                    removed = true;
                }
            }

            return removed;
        }

        public bool RemoveHeader(HttpHeader header)
        {
            return lstHeaders.Remove(header);
        }

        public bool ExistHeader(string name, bool caseSensitive)
        {
            for (int i = 0; i < lstHeaders.Count; i++)
            {
                if (caseSensitive && lstHeaders[i].name == name)
                    return true;
                else if (!caseSensitive && lstHeaders[i].name.ToUpper() == name.ToUpper())
                    return true;
            }

            return false;
        }

        public int NumberOfHeaderOcurrences(string headerName, bool caseSensitive)
        {
            int ocurrences = 0;

            for (int i = 0; i < lstHeaders.Count; i++)
            {

                if (caseSensitive && lstHeaders[i].name == headerName)
                    ocurrences++;
                else if (!caseSensitive && lstHeaders[i].name.ToUpper() == headerName.ToUpper())
                    ocurrences++;
            }

            return ocurrences;
        }
    }
}
